#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qfiledialog.h>
#include <qdebug.h>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    visionMatch=new VisionMatch;
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_browseBaseMap_clicked()
{
    //定义文件对话框类
       QFileDialog *fileDialog = new QFileDialog(this);
       fileDialog->resize(400,500);
       //定义文件对话框标题
       fileDialog->setWindowTitle(tr("选择文件"));
       //设置默认文件路径
       fileDialog->setDirectory("/agvwork/AgvFundation/fundation/data/robotData1/map/");
       //设置文件过滤器
       fileDialog->setNameFilter(tr("pgm(*.pgm)"));
       //设置可以选择多个文件,默认为只能选择一个文件QFileDialog::ExistingFiles
       fileDialog->setFileMode(QFileDialog::ExistingFiles);
       //设置视图模式
       fileDialog->setViewMode(QFileDialog::Detail);
       //打印所有选择的文件的路径
       QStringList fileNames;
       if(fileDialog->exec())
       {
           fileNames = fileDialog->selectedFiles();
       }
        qDebug()<<fileNames;
        if(fileNames.size()>0)
        {
            ui->lineEdit_baseMap->setText(fileNames[0]);
        }
}

void MainWindow::on_pushButton_browserLocalMap_clicked()
{
    //定义文件对话框类
       QFileDialog *fileDialog = new QFileDialog(this);
       fileDialog->resize(400,500);
       //定义文件对话框标题
       fileDialog->setWindowTitle(tr("选择文件"));
       //设置默认文件路径
       fileDialog->setDirectory("/agvwork/AgvFundation/fundation/data/robotData1/map/");
       //设置文件过滤器
       fileDialog->setNameFilter(tr("pgm(*.pgm)"));
       //设置可以选择多个文件,默认为只能选择一个文件QFileDialog::ExistingFiles
       fileDialog->setFileMode(QFileDialog::ExistingFiles);
       //设置视图模式
       fileDialog->setViewMode(QFileDialog::Detail);
       //打印所有选择的文件的路径
       QStringList fileNames;
       if(fileDialog->exec())
       {
           fileNames = fileDialog->selectedFiles();
       }
        qDebug()<<fileNames;
        if(fileNames.size()>0)
        {
            ui->lineEdit_localMap->setText(fileNames[0]);
        }
}

void MainWindow::on_pushButton_loadBaseMap_clicked()
{
    visionMatch->loadBaseMap(ui->lineEdit_baseMap->text());
}

void MainWindow::on_pushButton_loadLocalMap_clicked()
{
    visionMatch->loadLocalMap(ui->lineEdit_localMap->text());
}

void MainWindow::on_pushButton_calculate_clicked()
{
    visionMatch->calculateMatch();
    double x,y,r;
    bool isSucess;
    visionMatch->getLaserPositionPix( x,y,r,isSucess);
    ui->lineEdit_x->setText(QString::number(x,'f',3));
    ui->lineEdit_y->setText(QString::number(y,'f',3));
    ui->lineEdit_r->setText(QString::number(r,'f',3));
    if(true==isSucess)
    {
        ui->label_isSucess->setText("sucess");
    }
    else
    {
        ui->label_isSucess->setText("failed");
    }
}

void MainWindow::on_pushButton_drawPosition_clicked()
{
    visionMatch->showMatchWindow();
}

void MainWindow::on_pushButton_setBaseCompareMap_clicked()
{
    visionMatch->setBaseMapRegion(ui->lineEdit_baseOriginX->text().toDouble(),
                                  ui->lineEdit_baseOriginY->text().toDouble(),
                                 ui->lineEdit_baseWidth->text().toDouble(),
                                  ui->lineEdit_baseHeight->text().toDouble() );

}

void MainWindow::on_pushButton_setLocalCompareMap_clicked()
{
    visionMatch->setLocalMapRegion(ui->lineEdit_baseOriginX_2->text().toDouble(),
                                  ui->lineEdit_baseOriginY_2->text().toDouble(),
                                 ui->lineEdit_baseWidth_2->text().toDouble(),
                                  ui->lineEdit_baseHeight_2->text().toDouble() );

    visionMatch->setLaserPosInLocalMap(ui->lineEdit_x_2->text().toDouble(),
                                     ui->lineEdit_y_2->text().toDouble(),
                                     ui->lineEdit_r_2->text().toDouble());

}

void MainWindow::on_pushButton_showBaseCompareMap_clicked()
{
    visionMatch->showBaseCompareMap();
}

void MainWindow::on_pushButton_setLocalCompareMap_2_clicked()
{
    visionMatch->showLocalCampareMap();
}
